package com.sky.controller.admin;


import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetMealService;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;
import java.util.List;


/**
 * @Author FengHuan Wang
 * @Date 2024/12/30 18:57
 * @Version 1.0
 */
@RestController
@RequestMapping("/admin/setmeal")
@Api("套餐相关接口")
@Slf4j
public class SetMealController {
    @Autowired
    private SetMealService setMealService;

    /*新增套餐功能实现*/
    @PostMapping
    @ApiOperation("新增套餐")
    @CacheEvict(cacheNames = "setmealCache",key = "#setmealDTO.categoryId")
    public Result add(@RequestBody SetmealDTO setmealDTO){
        log.info("开始新增套餐");
        setMealService.add(setmealDTO);
        return Result.success();
    }

    /*套餐分页查询功能实现*/
    @GetMapping("/page")
    @ApiOperation("套餐分页查询")
    public Result<PageResult> pageQuery(SetmealPageQueryDTO setmealPageQueryDTO){
        log.info("开始套餐分页查询");
        PageResult result =  setMealService.pageQuery(setmealPageQueryDTO);
        return Result.success(result);
    }

    /*批量删除套餐功能实现*/
    @DeleteMapping
    @ApiOperation("批量删除套餐")
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result deleteByIds(@RequestParam List<Long> ids){
        log.info("开始批量删除套餐");
        setMealService.deleteByIds(ids);
        return Result.success();
    }


    /*查询回显*/
    @GetMapping("/{id}")
    @ApiOperation("查询回显")
    public Result<SetmealVO> getById(@PathVariable Long id){
        SetmealVO setmealVO = setMealService.getById(id);
        return Result.success(setmealVO);
    }

    /*修改套餐功能实现*/
    @PutMapping
    @ApiOperation("修改套餐功能")
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result update(@RequestBody SetmealVO setmealVO){
        log.info("开始修改套餐");
        setMealService.update(setmealVO);
        return Result.success();
    }

    /*套餐起售停售*/
    @PostMapping("/status/{status}")
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result stopAndStart(@PathVariable Integer status ,Long id){
        setMealService.stopAndStart(status,id);
        return Result.success();
    }
}
